package com.avalon.shop.service;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.model.RecordRow;
import com.avalon.core.util.JacksonUtil;
import com.avalon.core.util.RestTemplateUtils;
import com.avalon.shop.ShopContext;
import com.avalon.shop.config.ShopConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CommentService extends BaseService {
    public CommentService(ShopConfig shopConfig, ShopContext shopContext) {
        super(shopConfig, shopContext);
    }


    public void addOrderComment(List<Map<String, Object>> comments) {
        for (Map<String, Object> comment : comments) {
            Map<String, Object> value = new HashMap<>();
            value.put("comment", comment.get("comment"));
            value.put("commodityId", comment.get("commodityId"));
            value.put("userId", comment.get("userId"));
            value.put("orderId", comment.get("orderId"));
            value.put("rate", comment.get("rate"));
            value.put("anonymous", comment.get("anonymous"));
            addModel(value);
        }
    }

    @Override
    protected String getServiceName() {
        return "pet.commodity.comment";
    }

    public Map getLastProductComment(Integer productId) {
        List<Map> createTimeDesc = getModelPage("id, comment,userId.avatar, userId.name, commodityId.name, " +
                        "orderId.orderNo, rate, anonymous, createTime",
                1,
                1,
                "(&,(=,commodityId," + productId + "),(=,anonymous,false)",
                "createTime desc");

        if (createTimeDesc.isEmpty()) {
            return new HashMap();
        }
        return createTimeDesc.get(0);
    }

    public Map getProductCommentCount(Integer productId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("fields", "id");
        param.put("rpnCondition", "(=,commodityId," + productId + ")");


        param.put("serviceName", getServiceName());
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/get/all",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            List<Map> maps = JacksonUtil.convert2Object(responseEntity.getBody(),
                    new TypeReference<List<Map>>() {
                    });
            Map<String, Object> result = new HashMap<>();
            result.put("count", maps.size());
            return result;
        } else {
            log.error("getProductCommentCount 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }
    

    public List<Map> getProductCommentPage(Integer productId,
                                           Integer pageNum,
                                           Integer pageSize) {
        return getModelPage("id, comment, userId.name, commodityId.name, " +
                        "orderId.orderNo, rate, anonymous, createTime",
                pageNum,
                pageSize,
                "(=,commodityId," + productId + ")",
                "createTime desc");
    }
}
